#!/usr/bin/perl

use strict;

use lib qw( lib t/lib );
use SQLiteTest;
use DataValidatorSchema;
use Data::Dumper;
use Test::More tests => 3;

my $schema = SQLiteTest::get_test_schema( "DataValidatorSchema" );
my $id = 10;

ok( test_update(), "Test normal update" );
ok( test_update_validated_ok(), "Test update with validation and correct data" );
ok( test_update_validated_err(), "Test update with validation and invalid data" );
#ok( test_update(), "Test normal creation" );



sub test_update {
    my $test = $schema->resultset( 'Foo' )->first;
    $test->update( { name => 'just update' } );
    return 1;
}

sub test_update_validated_ok {
    my $test = $schema->resultset( 'Foo' )->first;
    eval {
        $test->update_validated( { name => 'foo is correct' } );
    };
    return $@ ? 0 : 1;
}

sub test_update_validated_err {
    my $test = $schema->resultset( 'Foo' )->first;
    eval {
        $test->update_validated( { name => 'not correct' } );
    };
    if ( my $e = $@ ) {
        my @e = $e->invalid_and_missing;
        return scalar @e == 1 && $e[0] eq 'name';
    }
    return 0;
}

1;
